package com.cloudpark.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cloudpark.common.util.ObjectUtil;
import com.cloudpark.common.util.Pagination;
import com.cloudpark.model.entity.Estate;
import com.cloudpark.model.entity.EstateWechat;
import com.cloudpark.model.mapper.EstateMapper;
import com.cloudpark.model.mapper.EstateWechatMapper;
import com.cloudpark.service.EstateService;

@Service
public class EstateServiceImpl implements EstateService {

    @Autowired
    private EstateMapper estateMapper;
    @Autowired
    private EstateWechatMapper estateWechatMapper;

    @Override
    public Pagination queryEstateList(Map<String, String> params, int start, int length, Long estateId) {
        Pagination pagination = new Pagination();
        String name = null == params.get("name") ? "" : params.get("name").toString();
        String tel = null == params.get("tel") ? "" : params.get("tel").toString();
        Estate paramVo = new Estate();
        if (StringUtils.isNotBlank(name)) {
            paramVo.setName(name);
        }
        if (StringUtils.isNotBlank(tel)) {
            paramVo.setTel(tel);
        }
        paramVo.setEstateId(estateId);
        int totalRows = estateMapper.countBySelective(paramVo);
        paramVo.setCurrentPage(start / length + 1);
        paramVo.setNumPerPage(length);
        paramVo.setTotalRows(totalRows);
        paramVo.initPageParams();
        pagination.setTotalRows(totalRows);
        List<Estate> resultList = estateMapper.selectBySelective(paramVo);
        pagination.setResultList(resultList);
        return pagination;
    }

    @Override
    public Estate queryEstateById(Long id) {
        return estateMapper.selectByPrimaryKey(id);
    }
    
	@Override
	public Estate queryEstateByCode(String code) {
		List<Estate> list = estateMapper.selectByCode(code);
		if (ObjectUtil.isNotBlank(list)) {
			return list.get(0);
		}
		return null;
	}

	@Override
    public void saveOrUpdate(Estate estate) {
        if (null != estate.getId() && estate.getId() > 0) {
            estateMapper.updateByPrimaryKey(estate);
        } else {
            estateMapper.insertSelective(estate);
        }
    }

	@Override
	public EstateWechat queryEstateWechatByDeveloperId(String developerId) {
	    List<EstateWechat> list = estateWechatMapper.selectByDeveloperId(developerId);
	    if (ObjectUtil.isBlank(list)) {
	    	return null;
	    }
		return list.get(0);
	}

	@Override
	public EstateWechat queryEstateWechatByEstateId(Long estateId) {
		List<EstateWechat> list = estateWechatMapper.selectByEstateId(estateId);
		if (ObjectUtil.isBlank(list)) {
	    	return null;
	    }
		return list.get(0);
	}

	@Override
	public EstateWechat queryEstateWechatById(Long id) {
		return estateWechatMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public EstateWechat queryEstateWechatByToken(String token) {
		List<EstateWechat> list = estateWechatMapper.selectByToken(token);
		if (ObjectUtil.isBlank(list)) {
	    	return null;
	    }
		return list.get(0);
	}

	@Override
	public void saveOrUpdateWechat(EstateWechat estateWechat) {
		if (ObjectUtil.isBigThanZero(estateWechat.getId())) {
			estateWechatMapper.updateByPrimaryKey(estateWechat);
		} else {
			estateWechatMapper.insert(estateWechat);
		}
	}

	@Override
	public String queryEstateOptions() {
		List<Estate> list = estateMapper.findAll();
		if (null != list && list.size() >= 1) {
			StringBuilder sb = new StringBuilder();
			for (Estate estate : list) {
				sb.append("<option value=\"").append(estate.getId()).append("\">").append(estate.getName()).append("</option>");
			}
			return sb.toString();
		}
		return null;
	}

}
